package admin

import (
	"anban/service"
	"anban/utils"
	"github.com/astaxie/beego"
	"html/template"
	"strconv"
)

type SchoolController struct {
	BaseController
}

func (c *SchoolController) Add() {
	p := map[string]interface{}{}
	p["level"] = 1
	_, c.Data["provinceList"] = service.GetRegionList(p)
	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/school/add.html"
}

func (c *SchoolController) DoAdd() {
	var res *utils.ResJsonStruct
	input := c.Input()
	if len(input["province_id"][0]) <= 0 {
		res = utils.ResJson(0, "请选择所属省", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["city_id"][0]) <= 0 {
		res = utils.ResJson(0, "请选择所属市", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["district_id"][0]) <= 0 {
		res = utils.ResJson(0, "请选择所属区/县", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["name"][0]) <= 0 {
		res = utils.ResJson(0, "请输入学校名称", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	_, err := service.AddSchool(input)
	if err != nil {
		res = utils.ResJson(0, "添加失败", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	res = utils.ResJson(1, "添加成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}

func (c *SchoolController) Edit() {
	id := c.Ctx.Input.Param(":id")

	p := map[string]interface{}{}
	p["level"] = 1
	_, c.Data["provinceList"] = service.GetRegionList(p)

	p = map[string]interface{}{}
	p["id"] = id
	record := service.GetSchoolInfo(p)
	c.Data["record"] = record

	p = map[string]interface{}{}
	p["level"] = 2
	p["parentId"] = record.Province.Id
	_, c.Data["cityList"] = service.GetRegionList(p)

	p = map[string]interface{}{}
	p["level"] = 3
	p["parent_id"] = record.City.Id
	_, c.Data["districtList"] = service.GetRegionList(p)

	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/school/edit.html"
}

func (c *SchoolController) DoEdit() {
	var res *utils.ResJsonStruct
	input := c.Input()
	if len(input["province_id"][0]) <= 0 {
		res = utils.ResJson(0, "请选择所属省", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["city_id"][0]) <= 0 {
		res = utils.ResJson(0, "请选择所属市", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["district_id"][0]) <= 0 {
		res = utils.ResJson(0, "请选择所属区/县", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["name"][0]) <= 0 {
		res = utils.ResJson(0, "请输入学校名称", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	_, err := service.EditSchool(input)
	if err != nil {
		res = utils.ResJson(0, "修改失败", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	res = utils.ResJson(1, "修改成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}

func (c *SchoolController) ShowList() {
	curPage, _ := strconv.Atoi(c.GetString("p"))
	if curPage <= 0 {
		curPage = 1
	}
	perCount, _ := beego.AppConfig.Int("percount")
	symPageCount, _ := beego.AppConfig.Int("symmetricpagecount")
	p := map[string]interface{}{}
	provinceId := c.GetString("province_id")
	if len(provinceId) > 0 {
		p["provinceId"] = provinceId
		c.Data["provinceId"] = utils.Atoi64(provinceId)
	} else {
		c.Data["provinceId"] = -1
	}
	cityId := c.GetString("city_id")
	if len(cityId) > 0 {
		p["cityId"] = cityId
		c.Data["cityId"] = utils.Atoi64(cityId)
	} else {
		c.Data["cityId"] = -1
	}
	districtId := c.GetString("district_id")
	if len(districtId) > 0 {
		p["districtId"] = districtId
		c.Data["districtId"] = utils.Atoi64(districtId)
	} else {
		c.Data["districtId"] = -1
	}
	name := c.GetString("name")
	c.Data["name"] = name
	if len(name) > 0 {
		p["name"] = name
	}
	p["relation"] = true
	p["convert"] = true
	p["curPage"] = curPage
	p["perCount"] = perCount
	totalCount, recordList := service.GetSchoolList(p)
	paginator := utils.NewPaginator(int(totalCount), perCount, symPageCount, curPage)
	c.Data["paginator"] = paginator.GetPageHtml()
	c.Data["recordList"] = recordList

	p = map[string]interface{}{}
	p["level"] = 1
	_, c.Data["provinceList"] = service.GetRegionList(p)
	if len(provinceId) > 0 {
		p = map[string]interface{}{}
		p["level"] = 2
		p["parentId"] = provinceId
		_, c.Data["cityList"] = service.GetRegionList(p)
	}
	if len(cityId) > 0 {
		p = map[string]interface{}{}
		p["level"] = 3
		p["parentId"] = cityId
		_, c.Data["districtList"] = service.GetRegionList(p)
	}

	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/school/list.html"
}

func (c *SchoolController) EditInfo() {
	userAdminId := c.GetSession("userAdminId")
	p := map[string]interface{}{}
	p["id"] = userAdminId
	p["relation"] = true
	userAdmin := service.GetUserAdminInfo(p)
	id := userAdmin.UserAdminRole.School.Id

	p = map[string]interface{}{}
	p["id"] = id
	c.Data["record"] = service.GetSchoolInfo(p)

	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/school/edit_info.html"
}

func (c *SchoolController) DoEditInfo() {
	var res *utils.ResJsonStruct
	input := c.Input()

	if len(input["morning_time"][0]) <= 0 {
		res = utils.ResJson(0, "请选择上午上课时间", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["afternoon_time"][0]) <= 0 {
		res = utils.ResJson(0, "请选择下午上课时间", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if _, ok := input["is_use_mobile_msg"]; !ok {
		input["is_use_mobile_msg"] = []string{"0"}
	}
	if _, ok := input["is_use_access_control"]; !ok {
		input["is_use_access_control"] = []string{"0"}
	}
	if _, ok := input["is_use_school_bus"]; !ok {
		input["is_use_school_bus"] = []string{"0"}
	}
	if _, ok := input["is_use_school_bus"]; !ok {
		input["is_use_school_bus"] = []string{"0"}
	}
	if _, ok := input["is_use_homework"]; !ok {
		input["is_use_homework"] = []string{"0"}
	}
	if _, ok := input["is_use_meeting"]; !ok {
		input["is_use_meeting"] = []string{"0"}
	}
	if _, ok := input["is_use_grade"]; !ok {
		input["is_use_grade"] = []string{"0"}
	}
	if _, ok := input["is_use_course"]; !ok {
		input["is_use_course"] = []string{"0"}
	}

	_, err := service.EditSchool(input)
	if err != nil {
		res = utils.ResJson(0, "修改失败", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	res = utils.ResJson(1, "修改成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}